package com.alan.springbootbase.controller;

import com.alan.springbootbase.dao.UserBaseRepository;
import com.alan.springbootbase.dao.UserRepository;
import com.alan.springbootbase.entity.User;
import com.alan.springbootbase.utils.ResultVoUtil;
import com.alan.springbootbase.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author Alan
 * @Description
 * @date 2021年01月03日 16:58
 */
@Slf4j
@RestController
public class HelloWorldController {

    @Value("${spring.profiles.active}")
    private String active;

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserBaseRepository userBaseRepository;


    @RequestMapping("/hello")
    public String index() {
        log.debug("debug Hello World");
        return "Hello World "+active;
    }

    @RequestMapping("/save")
    public String save() {
        log.debug("debug save");

        //通过Random类来获取随机数
        Random random = new Random();

        User user=new User();
        user.setUserName("aaaaa"+random.nextInt(10000));
        user.setPassWord("aaap");
        user.setEmail("aa"+random.nextInt(10000)+"@sccba.org");
        user.setState("01");
        user.setInfo("666666666");

        user.setDeleted(1);
        user.setBd(BigDecimal.valueOf(11.12));

        user.setTTime(new Date());
        user.setTDate(new Date());
        user.setTDatetime(new Date());

        userRepository.save(user);
        return "Hello World "+user.toString();
    }

    @RequestMapping("/get")
    public User get() {
        log.debug("debug Hello World");
        return  userRepository.findByUserName("aaaaa6619");
    }

    @RequestMapping("/getbase")
    public User getbase() {
        log.debug("debug Hello World");
        return  userBaseRepository.findByUserName("aaaaa6619");
    }

    @RequestMapping("/findLikeUserName/{name}")
    public List<User> findLikeUserName(@PathVariable String name) {
        log.debug("debug Hello World");
        return  userRepository.findLikeUserName(name);
    }
    @RequestMapping("/findLikeUserName1/{name}")
    public String findLikeUserName1(@PathVariable String name) {
        log.debug("debug Hello World");
        return  userRepository.findLikeUserName1(name).toString();
    }

    @GetMapping(value = "/get/{id}")
    public User get(@PathVariable int id) {
        log.debug("debug Hello World");
        return userRepository.getOne(id);
    }

    @GetMapping(value = "/findAll")
    public ResultVo findAll() {
        log.debug("debug Hello World");
        Pageable pageable= PageRequest.of(1,5);
        return ResultVoUtil.success(userRepository.findAll(pageable));

    }
    @GetMapping(value = "/findAllOrder")
    public List<User> findAllOrder() {
        log.debug("debug Hello World");
        Sort sort = Sort.by(Sort.Direction.DESC, "id");

        return userRepository.findAllByState(sort,"01");
    }


    @RequestMapping("/update")
    public User update() {
        log.debug("debug Hello World");

        User aa=userRepository.findByUserName("a");
        aa.setGmtModified(new Date());
        userRepository.save(aa);
        return  userRepository.save(aa);
    }

}
